package com.lq.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lq.gulimall.product.entity.PmsAttrEntity;
import com.lq.gulimall.product.service.PmsAttrAttrgroupRelationService;
import com.lq.gulimall.product.service.PmsAttrService;
import com.lq.gulimall.product.service.PmsCategoryService;
import com.lq.gulimall.product.vo.AttrGroupRelationVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lq.gulimall.product.entity.PmsAttrGroupEntity;
import com.lq.gulimall.product.service.PmsAttrGroupService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 属性分组
 *
 * @author lq
 * @email
 * @date 2020-11-09 19:28:37
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;
    @Autowired
    private PmsCategoryService categoryService;
    @Autowired
    private PmsAttrService attrService;
    @Autowired
    private PmsAttrAttrgroupRelationService relationService;


    //  /{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R arrtration(@PathVariable("attrgroupId") Long attrgroupId) {
        List<PmsAttrEntity> entities = attrService.getRlationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     *  关联里的分页查询 没有关联的数据
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
        public R listniattr(@PathVariable("attrgroupId") Long attrgroupId,
            @RequestParam Map<String, Object> params){
             PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = pmsAttrGroupService.queryPage(params);
        PageUtils page = pmsAttrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        Long catelogId = pmsAttrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        pmsAttrGroup.setCatelogPath(path);
        return R.ok().put("pmsAttrGroup", pmsAttrGroup);
    }

    /**
     *  新增关联
     * @param vos
     * @return
     */
        ///attr/relation
     @PostMapping("/attr/relation")
     public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
         relationService.saveBatch(vos);
     return R.ok();
     }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
        pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup) {
        pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }
    /**
     *  删除
     */
    @PostMapping("/attr/relation/delete")
      public R  deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
         attrService.deleteRelation(relationVos);
         return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
